<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use ZigaStrgar\Orderable\Orderable;

/**
 * App\Land
 *
 * @property mixed $standard_id
 * @property-read \App\Standard $standard
 * @mixin \Eloquent
 * @property int $id
 * @property string|null $code 土地编号
 * @property string|null $p_district_id
 * @property string|null $p_city_id 区域
 * @property string|null $p_province_id 区域1
 * @property string|null $slider 轮播图
 * @property string|null $title 标题
 * @property string|null $rent 租金
 * @property int|null $price 价格：25元/亩
 * @property string|null $certificate 土地证
 * @property string|null $area 面积
 * @property string|null $property 土地性质
 * @property string|null $use 规划用途
 * @property string|null $address 地址
 * @property string|null $property_cert 产权证：0为无，1为有
 * @property string|null $rest_term 剩余土地年限
 * @property string|null $owner_release 是否业主发布：0为不是1为是
 * @property int|null $click_count 点击量
 * @property int|null $call_count d电话拨打量
 * @property string|null $telephone 联系电话
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 * @property string|null $tag 标签
 * @property string|null $transaction_type 交易类型
 * @property string|null $is_recommend 是否推荐
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Land whereAddress($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Land whereArea($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Land whereCallCount($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Land whereCertificate($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Land whereClickCount($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Land whereCode($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Land whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Land whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Land whereIsRecommend($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Land whereOwnerRelease($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Land wherePCityId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Land wherePDistrictId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Land wherePProvinceId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Land wherePrice($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Land whereProperty($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Land wherePropertyCert($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Land whereRent($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Land whereRestTerm($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Land whereSlider($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Land whereStandardId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Land whereTag($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Land whereTelephone($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Land whereTitle($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Land whereTransactionType($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Land whereUpdatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Land whereUse($value)
 * @property int|null $area_id 面积范围
 * @property string|null $latitude 经度
 * @property string|null $longitude 纬度
 * @property string|null $logo 首图
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Land whereAreaId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Land whereLatitude($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Land whereLogo($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Land whereLongitude($value)
 * @property string|null $uses 规划用途
 * @property string|null $descs
 * @property string|null $is_out
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Land whereDescs($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Land whereIsOut($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Land whereUses($value)
 */
class Land extends Model
{
    use Orderable;

    public function orderable(){
        return [
            //DESC,ASC
            //'id' => 'DESC',
            'is_out'=> 'ASC',
            'created_at' => 'DESC',
            'id'
        ];
    }
    public static function boot()
    {
        parent::boot();

        static::saving(function ($model) {
            if($model->logo==''){
                $arr = explode(',',$model->slider);
                if(count($arr)>=1 && substr($model->slider,0,1)==','){
                    $model->logo = $arr[1];
                }else{
                    $model->logo = $arr[0];
                }
            }
            $area = $model->area;
            $area_id = 5;
            if($area<5000){
                $area_id = 1;
            }else if($area<10000){
                $area_id = 2;
            }else if($area<30000){
                $area_id = 3;
            }else if($area<50000){
                $area_id = 4;
            }
            $model->area_id = $area_id;

        });
    }
    //
    protected $table = 'land';
    public function standard(){
        return $this->belongsTo(Standard::class,'standard_id');
    }
    public function getStandardIdAttribute($value)
    {
        return explode(',', $value);
    }

    public function setStandardIdAttribute($value)
    {
        $this->attributes['standard_id'] = implode(',', $value);
    }
    public static function getSelectOptions()
    {
        $options = \DB::table('land')->select('id','title as text')->get();
        $selectOption = [];
        foreach ($options as $option){
            $selectOption[$option->id] = $option->text;
        }
        return $selectOption;
    }

}
